package com.ssy.wireless.task;

import com.ssy.wireless.api.service.*;
import com.ssy.wireless.entity.ApDeviceRecover;
import com.ssy.wireless.entity.LocalResource;
import com.ssy.wireless.entity.MessageAuth;
import com.ssy.wireless.entity.UserAuth;
import com.ssy.wireless.mapper.ApDeviceMapper;
import com.ssy.wireless.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 只为模拟数据做的定时器(后续有真正的接口时,必须删除)
 * Created by cofey on 16/11/23.
 */
//@Component
@Slf4j
public class SimulationDataTask {

    @Autowired
    private ApiApDeviceFaultService faultService;
    @Autowired
    private ApiApDeviceRecoverService recoverService;
    @Autowired
    private ApiApDeviceTrafficService trafficService;
    @Autowired
    private ApiLocalResourceService localResourceService;
    @Autowired
    private ApiMessageAuthService messageAuthService;
    @Autowired
    private ApiUserAuthService userAuthService;

    @Autowired
    private ApDeviceMapper apDeviceMapper;

    private List<String> apIndexs = new ArrayList<>();

    public void init() {
        if (apIndexs.size() < 1) {
            apIndexs = apDeviceMapper.selectApIndexsByApDeviceIds(random(2, 1072, 100));
        }
    }

    @Transactional
    //每10分钟 执行一次流量记录上传（随机选择几个设备）
    @Scheduled(fixedRate = 10 * 60 * 1000)
    public void execApDeviceTraffic() {
        log.info("流量上传");
        init();
        long[] suffixs = random(100, 9999, 100);
        int num = (int) (Math.random() * (99 - 1)) + 1;
        log.info("生成个数:" + num);
        for (int i = 0; i < num; i++) {
            trafficService.insertTraffic(apIndexs.get(i), Double.valueOf(num), Double.valueOf(suffixs[i]));
        }
    }

    @Transactional
    //每10分钟 执行一次用户、短信认证记录上传（随机选择几个设备）
    @Scheduled(fixedRate = 10 * 60 * 1000)
    public void execUserAuth() {
        log.info("用户认证");
        init();
        long[] suffixs = random(1000, 9999, 100);
        int num = (int) (Math.random() * (99 - 1)) + 1;
        log.info("生成个数:" + num);
        for (int i = 0; i < num; i++) {
            String mobile = i % 2 > 0 ? "181" + num + "82" + suffixs[i] : "18023" + num + suffixs[i];
            String userName = "t_" + suffixs[i];
            String ip = i % 2 > 0 ? "102." + num + ".4." + num : num + ".23.31" + num;
            String apIndex = apIndexs.get(i);
            UserAuth userAuth = new UserAuth();
            userAuth.setApIndex(apIndex);
            userAuth.setMobile(mobile);
            userAuth.setUserName(userName);
            userAuth.setIp(ip);
            userAuthService.insert(userAuth);

            MessageAuth messageAuth = new MessageAuth();
            messageAuth.setApIndex(apIndexs.get(i));
            messageAuth.setUserName(userName);
            messageAuth.setMobile(mobile);
            messageAuth.setIp(ip);
            messageAuthService.insertRequestInfo(messageAuth);

            messageAuth = new MessageAuth();
            messageAuth.setApIndex(apIndexs.get(i));
            messageAuth.setMobile(mobile);
            messageAuth.setSendTime(DateUtils.minuteAdd(DateUtils.getTime(), (num * 1)));
            try {
                messageAuthService.updateSendInfo(messageAuth);
            } catch (IllegalArgumentException e) {
                //忽略异常
                log.error("修改发送记录",e);
            }
            messageAuth = new MessageAuth();
            messageAuth.setApIndex(apIndexs.get(i));
            messageAuth.setMobile(mobile);
            messageAuth.setLoginTime(DateUtils.minuteAdd(DateUtils.getTime(), (num * 2)));
            messageAuth.setUpTime(DateUtils.minuteAdd(DateUtils.getTime(), (num * 2)));
            try {
                messageAuthService.updateLoginInfo(messageAuth);
            } catch (IllegalArgumentException e) {
                //忽略异常
                log.error("修改登录记录",e);
            }
            messageAuth = new MessageAuth();
            messageAuth.setApIndex(apIndexs.get(i));
            messageAuth.setMobile(mobile);
            messageAuth.setDownTime(DateUtils.minuteAdd(DateUtils.getTime(), (num * 3)));
            try {
                messageAuthService.updateDownInfo(messageAuth);
            } catch (IllegalArgumentException e) {
                //忽略异常
                log.error("修改下线记录",e);
            }
        }
    }

    @Transactional
    //每40分钟 执行一次本地资源上传
    @Scheduled(fixedRate = 40 * 60 * 1000)
    public void execLocalResource() {
        log.info("本地资源上传");
        int num = (int) (Math.random() * (10 - 1)) + 1;
        for (int i = 0; i < num; i++) {
            LocalResource localResource = new LocalResource();
            int type = (int) (Math.random() * (4 - 1)) + 1;
            localResource.setType(type);
            localResource.setImgName("测试文件");
            localResource.setIntroduce("测试描述");
            localResource.setStorePath("/data/file/ns/" + localResource.getType());
            localResource.setAuditor("u" + i);
            localResource.setEntName("南山科创局");
            localResourceService.insert(localResource);
        }
    }

    @Transactional
    //每4天 执行一次设备故障/维修记录上传（随机选择几个设备）
    @Scheduled(fixedRate = 4 * 24 * 60 * 60 * 1000)
    public void execApDeviceFaultAndRecover() {
        log.info("设备故障/维修记录上传");
        init();
        int num = (int) (Math.random() * (50 - 40)) + 40;
        log.info("生成个数:" + num);
        for (int i = 0; i < num; i++) {
            faultService.insert(apIndexs.get(i));
            ApDeviceRecover apDeviceRecover = new ApDeviceRecover();
            apDeviceRecover.setApIndex(apIndexs.get(i));
            apDeviceRecover.setRecoverTime(DateUtils.minuteAdd(DateUtils.getTime(), (num * 3)));
            recoverService.insert(apDeviceRecover);
        }
    }

    public static long[] random(int min, int max, int n) {
        if (n > (max - min + 1) || max < min) {
            return null;
        }
        long[] result = new long[n];
        int count = 0;
        while (count < n) {
            int num = (int) (Math.random() * (max - min)) + min;
            boolean flag = true;
            for (int j = 0; j < n; j++) {
                if (num == result[j]) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                result[count] = num;
                count++;
            }
        }
        return result;
    }
}
